package com.easylinkin.linkappapi.meterbilling.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.meterbilling.entity.RoomInfo;
import com.easylinkin.linkappapi.meterbilling.entity.ext.*;
import com.easylinkin.linkappapi.meterbilling.service.RoomExpensesService;
import com.easylinkin.linkappapi.meterbilling.service.RoomManagerService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * class info :房间管理
 *
 * @author liuqihang
 * @date 2021/7/8 11:54
 */
@RestController
@RequestMapping("/room")
public class RoomManagerController {

    @Resource
    private RoomExpensesService roomExpensesService;

    @Resource
    private RoomManagerService roomManagerService;
    @Value("${linkapp.configDeviceExecuteConditionCommand:}")
    BigDecimal configDeviceExecuteConditionCommand;

    @PostMapping("save")
    public RestMessage save(@RequestBody GlobalRoomInfo globalRoomInfo) {
        Assert.notNull(globalRoomInfo, "房间数据不能为空");
        Assert.notNull(globalRoomInfo.getRoomInfo(), "房间数据不能为空");
        Assert.notNull(globalRoomInfo.getRoomTenantryInfo(), "住户合同数据不能为空");
        String result = roomManagerService.saveRoomInfo(globalRoomInfo);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @PostMapping("update")
    public RestMessage update(@RequestBody GlobalRoomInfo globalRoomInfo) {
        Assert.notNull(globalRoomInfo, "房间数据不能为空");
        Assert.notNull(globalRoomInfo.getRoomInfo(), "房间数据不能为空");
        Assert.notNull(globalRoomInfo.getRoomTenantryInfo(), "住户合同数据不能为空");
        String result = roomManagerService.updateRoomInfo(globalRoomInfo);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @PostMapping("delete")
    public RestMessage delete(@RequestBody RoomInfo roomInfo) {
        Assert.notNull(roomInfo, "房间数据不能为空");
        Assert.notNull(roomInfo.getId(), "房间ID不能为空");
        roomManagerService.delete(roomInfo.getId());
        return RestBuilders.successBuilder().message("success").build();
    }

    @PostMapping("getInfo")
    public RestMessage getInfo(@RequestBody RoomInfo roomInfo) {
        Assert.notNull(roomInfo, "房间数据不能为空");
        Assert.notNull(roomInfo.getId(), "房间ID不能为空");
        GlobalRoomInfo globalRoomInfo = roomManagerService.getGlobalRoomInfo(roomInfo.getId());
        return RestBuilders.successBuilder().data(globalRoomInfo).build();
    }

    @PostMapping("batchUpdateRoomBilling")
    public RestMessage batchUpdateRoomBilling(@RequestBody BatchUpdateRoom batchUpdateRoom) {
        Assert.notNull(batchUpdateRoom, "批量修改数据不能为空");
        Assert.notNull(batchUpdateRoom.getRoomIds(), "房间ID数据不能为空");
        roomManagerService.batchUpdateRoomBilling(batchUpdateRoom);
        return RestBuilders.successBuilder().message("success").build();
    }

    @PostMapping("getRoomPage")
    public RestMessage getRoomPage(@RequestBody RequestModel<RoomList> requestModel) {
        Assert.notNull(requestModel, "requestModel 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        Assert.notNull(requestModel.getCustomQueryParams(), "查询参数 不能为空");
        IPage<RoomList> roomPage = roomManagerService.getRoomPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(roomPage).build();
    }

    @PostMapping("export")
    public void exportData(@RequestBody RoomList roomList, HttpServletRequest request, HttpServletResponse response) {
        roomManagerService.export(roomList, request, response);
    }

    @PostMapping("delTimer")
    public void deleteTimerTask() {
        roomManagerService.deleteTimerTask();
    }

    @PostMapping("getRoomListByResident")
    public RestMessage getRoomListByResident() {
        List<WechatRoomList> roomPage = roomManagerService.getRoomListByResident();
        return RestBuilders.successBuilder().data(roomPage).build();
    }

    @GetMapping("queryRoomData/{roomId}")
    public RestMessage queryRoomData(@PathVariable Integer roomId) {
        Assert.notNull(roomId, "roomId 不能为空");
        WechatRoomInfo wechatRoomInfo = roomManagerService.queryRoomData(roomId);
        return RestBuilders.successBuilder().data(wechatRoomInfo).build();
    }

    @GetMapping("getConfigDeviceExecuteConditionCommand")
    public RestMessage getConfigDeviceExecuteConditionCommand(){
        return RestBuilders.successBuilder().data(configDeviceExecuteConditionCommand).build();
    }

    @GetMapping("timerTask")
    public RestMessage timerTask(){
        roomExpensesService.meterTimerTask();
        return RestBuilders.successBuilder().message("success").build();
    }
}
